<?php

/**
 * Person form.
 *
 * @package    makethetouch
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class PersonForm extends BasePersonForm {

    public function configure() {
        unset(
                $this["level"],
                $this["subscriptiondate"],
                $this["subscriptionip"],
                $this["ip_inscription"],
                $this["my_club"],
                $this["status"]
        );
        $this->setWidgets(array(
            'login' => new sfWidgetFormInput(),
            'first_name' => new sfWidgetFormInput(),
            'last_name' => new sfWidgetFormInput(),
            'email' => new sfWidgetFormInput(),
            'password' => new sfWidgetFormInputPassword(),
            'repassword' => new sfWidgetFormInputPassword(),
            'country' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Country'), 'add_empty' => false, 'order_by' =>array('name','asc'))),
            'referee_level'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('RefereeLevel'), 'add_empty' => true, 'order_by' =>array('level','asc'))),
            'captcha' => new sfAnotherWidgetFormReCaptcha()
        ));
        
        $this->setValidators(array(
            'login' => new sfValidatorString(array('required' => true, 'min_length' => 3, 'max_length' => 20)),
            'first_name' => new sfValidatorString(array('required' => true, 'min_length' => 1, 'max_length' => 30)),
            'last_name' => new sfValidatorString(array('required' => true, 'min_length' => 1, 'max_length' => 30)),
            'email' => new sfValidatorEmail(array('required' => true)),
            'password' => new sfValidatorString(array('required' => true, 'min_length' => 6, 'max_length' => 20)),
            'repassword' => new sfValidatorString(array('required' => true, 'min_length' => 6, 'max_length' => 20)),
            'country'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Country'))),
            'referee_level'    => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('RefereeLevel'), 'required' => true)),
        ));

        $this->widgetSchema->setLabels(array(
            'login'         => 'Login',
            'first_name'    => 'First name',
            'last_name'     => 'Last name',
            'email'         => 'Email address',
            'password'      => 'Password',
            'repassword'    => 'Retype password',
            'country'       => 'Country',
            'referee_level' => 'Do you have a referee badge?',
            'captcha'       => 'Captcha'
        ));
        $this->validatorSchema->setPostValidator(new sfValidatorAnd(
                        array(
                            new sfValidatorSchemaCompare(
                                    'password', sfValidatorSchemaCompare::EQUAL, 'repassword',
                                    array(),
                                    array('invalid' => "The password and password confirmation are different")
                            ),
                            new sfAnotherValidatorSchemaReCaptcha($this, 'captcha'),
                            new sfValidatorCallback(array('callback' => array($this, 'checkAvailability')))
                ))
        );
        $this->widgetSchema->setFormFormatterName('list');
        $this->widgetSchema->setHelp('repassword', 'Retype the previous password to prevent errors.');
        $this->widgetSchema->setHelp('email', 'Require a valid email address.');
        $this->widgetSchema->setHelp('login', 'Login between 3 and 20 alphanumerics characters.');
        $this->widgetSchema->setNameFormat('person[%s]');

        $this->setDefault('country', 'en');
        $this->setDefault('referee_level', 'None');
    }

    public function checkAvailability($validator, $values) {
        if (!empty($values['login'])) {

            $nbr = Doctrine_Query::create()
                            ->from('Person p')
                            ->where("p.login = ?", $values['login'])
                            ->count();

            if ($nbr == 0) {
                // Login available
                return $values;
            } else {
                // Login not available
                throw new sfValidatorError($validator, 'This login is already used.');
            }
        }
    }

    public function updatePasswordColumn($value) {
        if (!empty($value)) {
            $value = md5($value);
        }

        return $value;
    }

}
